package com.auxgroup.hr.front.site.co.hireperson;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import com.auxgroup.hr.front.service.hireperson.HirePersonService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsCHireEducatT;
import com.auxgroup.hr.front.dao.domain.PsCHirePrimaryClass;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.hireperson.HireEducatService;
import com.auxgroup.hr.front.site.co.BaseCO;

/**
 * 入职教育经历CO
 * @author lvliuhao
 * @version 创建时间：180526 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/hireEducat")
public class HireEducatCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(HireEducatCO.class);

	@Autowired
	public HireEducatService hireEducatService;

	@Autowired
	private HirePersonService hirePersonService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	/**
	 * 获取教育经历
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(String rsHireId,BigDecimal cSeqno) {
		logger.info("getHireEducat param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"教育经历的seqno不能为空");
		}
		PsCHirePrimaryClass hirePrimary = new PsCHirePrimaryClass(cSeqno, rsHireId);
		PsCHireEducatT hireEducat = hireEducatService.getHireEducat(hirePrimary);
		return buildRespone(hireEducat);
	}
	
	/**
	 * 保存教育经历
	 * @return
	 */
	@PostMapping(value = "/save")
	public BaseResponseVO save(PsCHireEducatT hireEducat) {
		logger.info("saveHireEducat param= {}", hireEducat);
		if(StringUtils.isBlank(hireEducat.getRsHireId())){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(StringUtils.equalsIgnoreCase(hireEducat.getcYesNo1(),"Y")) {
			List<PsCHireEducatT> phts = hireEducatService.getAll(hireEducat.getRsHireId());
			for(PsCHireEducatT pht : phts) {
				if(StringUtils.equalsIgnoreCase(pht.getcYesNo1(),"Y")
						&& !pht.getcSeqno().equals(hireEducat.getcSeqno())) {
					return buildRespone(BaseRetCode.CODE_ERROR_INPUT_NOT_RIGHT.getRetCode(), "只能有一条最高学历记录");
				}
			}

			//如果为最高学历，把相关教育信息保存至人员基本信息表
			hirePersonService.updateEducatInfo(hireEducat);
		}

		if(hireEducat.getcSeqno()!=null){
			PsCHirePrimaryClass primary = new PsCHirePrimaryClass();
			primary.setcSeqno(hireEducat.getcSeqno());
			primary.setRsHireId(hireEducat.getRsHireId());
			PsCHireEducatT nowEdu = hireEducatService.getHireEducat(primary);
			if(nowEdu==null){
				hireEducat = hireEducatService.save(hireEducat);
			}else{
				objectMapper.map(hireEducat, nowEdu,"notCopyNull_hireEdu");
				hireEducat = hireEducatService.save(nowEdu);
			}
		}else{
			hireEducat = hireEducatService.save(hireEducat);
		}
		if(hireEducat.getcSeqno()!=null){
			return buildRespone(true);
		}
		return buildRespone(false);
		
	}
	
	/**
	 * 删除教育经历
	 * @return
	 */
	@PostMapping(value = "/del")
	public BaseResponseVO del(String rsHireId,BigDecimal cSeqno) {
		logger.info("delHireEducat param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"教育经历的seqno不能为空");
		}
		PsCHirePrimaryClass hirePrimary = new PsCHirePrimaryClass(cSeqno, rsHireId);
		Boolean isdel = hireEducatService.delete(hirePrimary);
		return buildRespone(isdel);
	}
	
	/**
	 * 获取全部教育经历
	 * @param rsHireId
	 * @return
	 */
	@GetMapping(value = "/getAll")
	public BaseResponseVO getAll(String rsHireId){
		logger.info("getHireEducat param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		List<PsCHireEducatT> list = hireEducatService.getAll(rsHireId);
		return buildRespone(list);
	}
	
	

}
